Failed Conditions
Push — master ( c3e11c...1d8d9e )
by Yo
02:09 queued 37s
created

cron.js ➔ job   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A cron.js ➔ ... ➔ next 0 9 4
A cron.js ➔ ... ➔ ???.onTick 0 4 1
1
var CRON = require('cron').CronJob;
2
3
// ------------------------------------------
4
//  CONSTRUCTOR
5
// ------------------------------------------
6
7
var init = function(){
8
  info('Starting CRONManager ...');
9
  return CRONManager;
10
}
11
12
/**
13
 * Starting all jobs from properties
14
 */
15
var start = function(){
16
  var plugins = SARAH.PluginManager.getList();
0 ignored issues
show
Bug introduced by
The variable SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
17
  for (var i = 0 ; i < plugins.length ; i++){
18
    var plugin = plugins[i]; 
19
    if (!plugin.cron) continue;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
20
    job(plugin);
21
  }
22
}
23
24
var job = function(plugin) {
25
  if (!plugin.cron.time){ return warn('Missing cron time table');}
26
  info("Starting new job %s with cron %s", plugin.name, plugin.cron.time); 
27
  
28
  // Build callback
29
  var next = function(data){
30
    if (!data){ return; }
31
    if (data.error){ SARAH.speak(tts); }
0 ignored issues
show
Bug introduced by
The variable tts seems to be never initialized.
Loading history...
Bug introduced by
The variable SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
32
    
33
    var tts = SARAH.ScriptManager.speak(data.tts);
34
    if (tts){ SARAH.speak(tts); }
35
    
36
    SARAH.RuleEngine.dispatch(plugin.name, data);
37
  }
38
  
39
  // Create job
40
  var job = new CRON({
0 ignored issues
show
Unused Code introduced by
The variable job seems to be never used. Consider removing it.
Loading history...
41
    cronTime: plugin.cron.time,
42
    onTick: function() {
43
      info('Cron: %s', plugin.name);
44
      plugin.getInstance().cron(next, plugin.cron, SARAH);
0 ignored issues
show
Bug introduced by
The variable SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
45
    },
46
    start: true
47
  });
48
  
49
  // Run once
50
  plugin.getInstance().cron(next, plugin.cron, SARAH);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
51
}
52
53
// ------------------------------------------
54
//  PUBLIC
55
// ------------------------------------------
56
57
var CRONManager = {
58
  'init' : init,
59
  'start': start
60
}
61
62
// Exports Manager
63
exports.init = CRONManager.init;